import service from './service'
import { CONTENT_TYPE } from '@/constants'
import { loginStateStoreWithOut } from '@/store/modules/loginState'
export const envConfig = import.meta.env
const request = (option: AxiosConfig) => {
  const { url, method, params, data, headers, responseType } = option
  const header = {
    'Content-Type': CONTENT_TYPE,
    ...headers
  }
  const userStore = loginStateStoreWithOut()
  const token = userStore.token
  if (token) {
    header['AccreditId'] = token
  }
  return service.request({
    url: url,
    method,
    params,
    data,
    responseType: responseType,
    headers: header
  })
}
const getRequestUri = (head) => {
  const uri = envConfig['VITE_API_' + head.toUpperCase() + '_PATH']
  if (uri) {
    return uri
  }
  return envConfig['VITE_API_DEF_PATH']
}
export default {
  get: <T = any>(option: AxiosConfig) => {
    return request({ method: 'get', ...option }) as Promise<T>
  },
  post: <T = any>(option: AxiosConfig) => {
    return request({ method: 'post', ...option }) as Promise<T>
  },
  cancelRequest: (url: string | string[]) => {
    return service.cancelRequest(url)
  },
  cancelAllRequest: () => {
    return service.cancelAllRequest()
  },
  getRequestUri
}
